﻿using AutoScaleHelper;
using PCSC;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tool
{
    public partial class Pcsc : Form
    {
        AutoScale autoScale = new AutoScale();

        private bool _isLoaded = false;

        public Pcsc()
        {
            InitializeComponent();
            autoScale.SetContainer(this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 创建一个新的智能卡上下文
            SCardContext context = new SCardContext();
            context.Establish(SCardScope.System);
            string[] readerNames = context.GetReaders();
            // 检查是否有可用的读卡器
            if (readerNames.Count() == 0)
            {
                MessageBox.Show("没有找到可用的智能卡读卡器。");
                return;
            }
            for (int i = 0; i < readerNames.Length; i++) 
            {
                string str = readerNames[i];
                MessageBox.Show(str);
            }


            // 连接到第一个读卡器
            string readerName = readerNames[0];
            SCardReader reader = new SCardReader(context);
            SCardError connectResult = reader.Connect(readerName, SCardShareMode.Shared, SCardProtocol.Any);
            if (connectResult != SCardError.Success)
            {
                MessageBox.Show("连接读卡器失败!");
            }



        }

        private void PCSC_SizeChanged(object sender, EventArgs e)
        {
            if (_isLoaded)
            {
                this.SuspendLayout();
                autoScale.UpdateControlsLayout();
                this.ResumeLayout();
            }
        }


        private void Pcsc_Load(object sender, EventArgs e)
        {
            _isLoaded = true;
        }
    }
}
